#include <windows.h>
#include "glew.h"
//#include "glut.h
#include "WSImage.h"
#include "WSSprite.h"
#include "WSAction.h"
#include "WSGlobal.h"

WS_USING_NS_CORE

LRESULT WndProc(HWND, UINT, WPARAM, LPARAM);

GLvoid ResizeGLScene(GLsizei width, GLsizei height)
{
	if (height == 0)
	{
		height = 1;
	}

	glViewport(0, 0, width, height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

int InitGL()
{
	WSGLState::Init();

	return TRUE;
}

void RenderTest();

int DrawGLScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	

	glLoadIdentity();

	RenderTest();

	return TRUE;								
}

HWND CreateGLWindow(wchar_t* title, int width, int height, int bits)
{
	GLuint pixelFormat;
	WNDCLASS wc;

	DWORD dwStyle, dwExStyle;
	RECT windowRect;

	windowRect.left = 0;
	windowRect.right = width;
	windowRect.top = 0;
	windowRect.bottom = height;

	wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc = (WNDPROC) WndProc;
	wc.hInstance = GetModuleHandle(NULL);
	wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.lpszClassName = L"WuShuang";
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = NULL;
	wc.lpszMenuName = NULL;

	bool ok = RegisterClass(&wc);

	dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
	dwStyle = WS_OVERLAPPEDWINDOW;

	AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);

	HWND hwnd = CreateWindowEx(	dwExStyle,
								L"WuShuang",
								title,
								dwStyle,
								0, 0,
								width, height,
								NULL, NULL,
								wc.hInstance,
								NULL);

	static PIXELFORMATDESCRIPTOR pfd = 
	{
		sizeof(PIXELFORMATDESCRIPTOR),
		1,
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA,
		bits,
		0, 0, 0, 0, 0, 0,
		0,
		0,
		0,
		0, 0, 0, 0,
		16,
		0,
		0,
		PFD_MAIN_PLANE,
		0,
		0, 0, 0
	};

	HDC hdc = GetDC(hwnd);
	
	pixelFormat = ChoosePixelFormat(hdc, &pfd);
	SetPixelFormat(hdc, pixelFormat, &pfd);

	HGLRC hRC = wglCreateContext(hdc);
	wglMakeCurrent(hdc, hRC);

	ShowWindow(hwnd, SW_SHOW);
	SetForegroundWindow(hwnd);
	SetFocus(hwnd);

	ResizeGLScene(width, height);

	InitGL();

	return hwnd;
}

LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_SIZE:
		ResizeGLScene(LOWORD(lParam), HIWORD(lParam));
		return 0;
	case WM_CLOSE:
		PostQuitMessage(0);
	default:
		return DefWindowProc(hwnd, msg, wParam, lParam);
	}
}

WSSprite* sprite;
WSTexture* texture;
void RunTest()
{
	WSImage image;
	image.Initialize("c:\\actor.png", WSImage::ImageFormat::PNG);
	texture = new WSTexture(&image);
	sprite = new WSSprite(texture, WSRect(0.0f, 0.0f, 4.0f, 4.0f));
}

void RenderTest()
{
	glTranslatef(0.f, 0.f, -20.f);
	sprite->Render();	
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg;
	BOOL done = FALSE;

	HWND hwnd = CreateGLWindow(L"WuShuang", 640, 480, 16);

	HDC hdc = GetDC(hwnd);

	RunTest();

	while (!done)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
			{
				done = TRUE;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else
		{
			DrawGLScene();
			SwapBuffers(hdc);
		}
	}
	
	return 0;
}
